package com.szp.springbootvue.controller;

import com.szp.springbootvue.domain.RoleUser;
import com.szp.springbootvue.domain.User;
import com.szp.springbootvue.mapper.RoleUserMapper;
import com.szp.springbootvue.mapper.UserMapper;
import com.szp.springbootvue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleUserMapper roleUserMapper;
//    @Autowired
//    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @PostMapping("/register")
    public String register(@RequestBody User user){
        System.out.println("user的"+user);
//        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        System.out.println("user的"+user);
        //插入数据库  -- 然后获得主键
        userMapper.insert(user);
        //设置用户角色信息 --- 往role_user中插入  用户默认为2 ok
        System.out.println(user.getUserId());
        RoleUser roleUser = new RoleUser();
        roleUser.setRoleId(2);  //默认为user角色
        roleUser.setUserId(user.getUserId());
        roleUserMapper.insert(roleUser);
        return "注册成功";
    }

    @GetMapping("/findAllUser")
    public List<User> userLogin(){
        return userService.findAllUser();
    }
    @GetMapping("getUser")
    public List<User> getUser(){
        return userService.findAllUser();
    }
}
